﻿using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Fields;
using Aspose.Words.MailMerging;
using EcoSystem.Office.Enums;
using EcoSystem.Office.Word;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace EcoSystem.Office
{
    /// <summary>
    /// 
    /// </summary>
    public class WordExportService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <param name="saveFormat"></param>
        /// <returns></returns>
        public static MemoryStream HtmlToWord(string htmlContent, DocSaveFormat saveFormat)
        {
            Document document = new Document();
            DocumentBuilder builder = new DocumentBuilder(document);
            builder.InsertHtml(htmlContent);
            MemoryStream stream = new MemoryStream();
            document.Save(stream, (SaveFormat)saveFormat);
            return stream;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="templateFile"></param>
        /// <param name="dataSource"></param>
        /// <param name="saveFormat"></param>
        /// <returns></returns>
        public static MemoryStream ExportForTemplate<T>(string templateFile, T dataSource, DocSaveFormat saveFormat)
        {
            FileInfo info = new FileInfo(templateFile);
            using (Stream stream = info.Open(FileMode.Open, FileAccess.Read))
            {
                return ExportForTemplate<T>(stream, dataSource, saveFormat);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="templateFile"></param>
        /// <param name="dataSource"></param>
        /// <param name="saveFormat"></param>
        /// <returns></returns>
        public static MemoryStream ExportForTemplate<T>(Stream templateStream, object dataSource, DocSaveFormat saveFormat)
        {
            Document document = new Document(templateStream);
            DocumentBuilder builder = new DocumentBuilder(document);

            var helper = new MailMergeHelper();
            FormMergeData fromData = helper.ModelToMergeFields(dataSource);
            var fields = fromData.Where(s => s.Type != FormFieldType.Complex).ToList();

            foreach (var item in fields)
            {
                if (item.Type == FormFieldType.Text)
                {
                    builder.MoveToMergeField(item.Name);
                    builder.Write(item.Value?.ToString());
                }
                else if (item.Type == FormFieldType.Image)
                {
                    builder.MoveToMergeField(item.Name);
                    Shape shape = builder.InsertImage(helper.GetImageData(item));
                    if (item.ImageOptions != null)
                    {
                        var opts = item.ImageOptions;
                        if (opts.Width > 0) { shape.Width = opts.Width; }
                        if (opts.Height > 0) { shape.Height = opts.Height; }
                    }
                }
            }

            //处理列表数据
            document.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
            var tables = fromData.Where(s => s.Type == FormFieldType.Complex).ToList();
            foreach (var item in tables)
            {
                var table = helper.FormMergeToTable((List<FormMergeData>)item.Value);
                table.TableName = item.Name;
                document.MailMerge.ExecuteWithRegions(table);
            }

            MemoryStream stream = new MemoryStream();
            document.Save(stream, (SaveFormat)saveFormat);
            return stream;
        }
    }

   





}
